package omg.loot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public class MouseReader implements MouseListener, MouseMotionListener, MouseWheelListener {
   public static final double FLING_THRESHOLD = 1d; //Threshold velocity for flinging (px/ms)
   //Key buffers:
   boolean right = false;
   boolean left = false;
   int x, y; //Mouse location
   int tileX;
   int tileY; //The index of the tile under the cursor, updated from Camera

   int zoom = 100; //Future zoom level (camera updates to this gradually)
   double scrollX = 0f, scrollY = 0f; //How many pixels the map has been dragged
   boolean isOnPlayfield = false; //Indicates if the cursor is on top of the playfield
   //Fling stuff:
   int fling_startX, fling_startY, fling_stopX, fling_stopY;
   int fling_tempX, fling_tempY;
   long fling_startTime, fling_stopTime;
   double fling_resultX, fling_resultY;

   public MouseReader() {}

   //MouseMotionListener:
   public void mouseMoved(final MouseEvent e) {
      x = (int) (e.getX() * Shared.scaleX);
      y = (int) (e.getY() * Shared.scaleY);
   }
   public void mouseDragged(final MouseEvent e) {
      if (right) {
         fling_tempX = x;
         fling_tempY = y;
         scrollX += e.getX() * Shared.scaleX - x;
         scrollY += e.getY() * Shared.scaleY - y;
         x = (int) (e.getX() * Shared.scaleX);
         y = (int) (e.getY() * Shared.scaleY);
      }
   }
   //MouseListener:
   public void mousePressed(final MouseEvent e) {
      x = (int) (e.getX() * Shared.scaleX);
      y = (int) (e.getY() * Shared.scaleY);
      if (e.getButton() == MouseEvent.BUTTON1) //Vasen
         left = true;
      else if (e.getButton() == MouseEvent.BUTTON3) { //Oikea
         right = true;
         fling_resultX = 0; //Stop the possible previous fling
         fling_resultY = 0;
         fling_startX = x;
         fling_startY = y;
         fling_startTime = System.nanoTime();
      }
   }
   public void mouseExited(final MouseEvent e) {}
   public void mouseEntered(final MouseEvent e) {}
   public void mouseReleased(final MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON1) //Vasen
         left = false;
      else if (e.getButton() == MouseEvent.BUTTON3) { //Oikea
         right = false;
         fling_stopX = (int) (e.getX() * Shared.scaleX);
         fling_stopY = (int) (e.getY() * Shared.scaleY);
         fling_stopTime = System.nanoTime();
         //Calculate the travelled distance:
         float fling_velocity = Shared.distance(fling_startX, fling_startY, fling_stopX, fling_stopY);
         //Calculate the velocity in px/ms;
         fling_velocity /= (fling_stopTime - fling_startTime)/Shared.divider;
         //If the velocity was over the threshold and the distance between last two updates was over 1px:
         if (fling_velocity >= FLING_THRESHOLD && Shared.distance(fling_tempX, fling_tempY, x, y) > 1) {
            fling_resultX = (fling_stopX - fling_startX) * fling_velocity * 0.1;
            fling_resultY = (fling_stopY - fling_startY) * fling_velocity * 0.1;
         }
      }
   }
   public void mouseClicked(final MouseEvent e) {}

   //MouseWheelListener:
   public void mouseWheelMoved(final MouseWheelEvent e) {
      zoom -= e.getWheelRotation()*Camera.ZOOM_STEP*100;
      if (zoom > Camera.ZOOM_MAX*100d)
         zoom = (int) (Camera.ZOOM_MAX*100d);
      else if (zoom < Camera.ZOOM_MIN*100d)
         zoom = (int) (Camera.ZOOM_MIN*100d);
   }

   /*
   public void edgeScroll() {
      double dist = calculateDistance();
      if (dist > Camera.SCROLL_RADIUS) {
         final double angle = Math.atan2((Main.CENTERY-y),(Main.CENTERX-x));
         dist = Math.min(1, (dist-Camera.SCROLL_RADIUS)/(Camera.SCROLL_RADIUSEDGE-Camera.SCROLL_RADIUS));
         scrollX = Math.cos(angle) * Camera.SCROLL_SPEED * dist;
         scrollY = Math.sin(angle) * Camera.SCROLL_SPEED * dist;
      }
      else {
         scrollX = 0f;
         scrollY = 0f;
      }
   }

   private double calculateDistance() {
      final double t_x = (x-Main.CENTERX)*((double)Main.HEIGHT/(double)Main.WIDTH);
      final double t_y = (y-Main.CENTERY);
      return Math.sqrt(t_x*t_x + t_y*t_y);
   }*/
}